import React, { Component } from 'react'

export default class Footer extends Component {
  render() {
    const { todos } = this.props
    // 已完成的个数
    // 数组reduce方法，reduce((pre,item)=> {},0),pre是自定义初始参数，0是pre的初始值，item是遍历的数组每一项
    const doneCount = todos.reduce((pre, item) => pre + (item.done ? 1 : 0), 0)
    return (
      <div className="todo-footer">
        <label>
          <input type="checkbox" onChange={this.handleCheckAll} checked={doneCount === todos.length && todos.length !== 0 ? true : false} />
        </label>
        <span>
          <span>已完成{doneCount}</span> / 全部{todos.length}
        </span>
        <button onClick={this.handleClearAllDone} className="btn btn-danger">清除已完成任务</button>
      </div>
    )
  }

  // 全选
  handleCheckAll = (e) => {
    this.props.checkAllTodo(e.target.checked)
  }

  // 删除选中
  handleClearAllDone = () => {
    if (window.confirm('确定删除吗？')) {
      this.props.clearAllDone()
    }
  }
}
